package org.ccheng.xztx.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.ccheng.xztx.service.WorldBossService;
import org.ccheng.xztx.util.Api;
import org.ccheng.xztx.util.ApiConstants;
import org.springframework.stereotype.Service;

@Service
public class WorldBossServiceImpl implements WorldBossService {

    private static final String I = "[世界BOSS]：";

    public JSONObject getInfo() {
        String response = Api.get(ApiConstants.API_WORLD_BOSS_INDEX);

        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOpt("name", ReUtil.get("(?<=<div class=\"gaps normal\">)\\S+(?=<)", response, 0));
        jsonObject.putOpt("healthPoint", ReUtil.get("(?<=血量：)\\d(?=/)", response, 0));
        jsonObject.putOpt("date", ReUtil.get("(?<=攻击时间)\\S+(?=<)", response, 0));

        return jsonObject;
    }

    public boolean PK() {
        JSONObject jsonObject = getInfo();

        String date = jsonObject.getStr("date");
        String startDate = date.split("-")[0];
        String endDate = date.split("-")[1];

        if (!DateUtil.isIn(DateUtil.date(), DateUtil.parse(startDate), DateUtil.parse(endDate))) {
            Api.out(I, "当前时间不在活动时间内 ", date);
            return false;
        }

        if (jsonObject.getLong("healthPoint") == 0) {
            Api.out(I, "BOSS生命值 0 活动结束");
            return false;
        }

        Api.out(I, "执行普通攻击");
        String response = Api.get(ApiConstants.API_WORLD_BOSS_NORMAL_PK);
        String result = ReUtil.get("获得\\S+(?=<br/>)|战斗CD中！推荐您使用另一种攻击手段！", response, 0);
        Api.out(I, result);

        return true;
    }

    public void auto() {
        if (!PK()) {
            Api.out(I, "攻击失败");
        }
    }

}
